package com.barneyx.mshinfoplatform.event.eventListener;

import cn.hutool.core.util.StrUtil;
import com.barneyx.mshinfoplatform.entity.admin.AdminLogEntity;
import com.barneyx.mshinfoplatform.entity.admin.AdminUserEntity;
import com.barneyx.mshinfoplatform.event.MshEvent;
import com.barneyx.mshinfoplatform.service.common.IAdminLogEntityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 管理员操作日志处理器
 */
@Component
@Slf4j
public class AdminOptionLogEventListener {
    @Resource
    private IAdminLogEntityService adminLogService;

    @EventListener(classes = MshEvent.class, condition = "#event.eventType.value.equalsIgnoreCase('EVENT_ADMIN_LOGIN_SUCCESS')")
    public void onAdminLoginSuccessEvent(MshEvent<AdminUserEntity> event) {
        adminLogService.save(AdminLogEntity.builder()
                .AdminUserId(event.getData().getAdminId())
                .LogContent(StrUtil.format("管理员用户:{},成功登陆了后台管理系统", event.getData().getUserName()))
                .build());
    }

    @EventListener(classes = MshEvent.class, condition = "#event.eventType.value.equalsIgnoreCase('EVENT_ADMIN_LOGIN_FAILD')")
    public void onAdminLoginFaildEvent(MshEvent<AdminUserEntity> event) {
        adminLogService.save(AdminLogEntity.builder()
                .AdminUserId(event.getData().getAdminId())
                .LogContent(StrUtil.format("管理员用户:{},登陆失败", event.getData().getUserName()))
                .build());
    }
}
